package config

import (
	"github.com/spf13/viper"
	"gluenet/pkg/database/mysql"
	"sync"
)

type Config struct {
	Debug          bool       `mapstructure:"debug"`
	MySQLConf      mysql.Conf `mapstructure:"mysql"`
	HttpPort       string     `mapstructure:"httpPort"`
	NatsServerAddr string     `mapstructure:"natsServerAddr"`
	EnableMetering bool       `mapstructure:"enableMetering"` //Start automatic metering
	EnableDeploy   bool       `mapstructure:"enableDeploy"`   //Start automatic deployment
	PromAddr       string     `mapstructure:"promAddr"`
	MaasUrl        string     `mapstructure:"maasUrl"`
	MaasAPIKey     string     `mapstructure:"maasApiKey"`
	MaasAPIVersion string     `mapstructure:"maasApiVersion"`
	MaasAgentName  string     `mapstructure:"maasAgentName"`
}

var c Config
var mutex sync.RWMutex

func InitByFilePath(cfgPath string) error {
	mutex.Lock()
	defer mutex.Unlock()
	viper.SetConfigFile(cfgPath)
	if err := viper.ReadInConfig(); err != nil {
		return err
	}

	return viper.Unmarshal(&c)
}

func GetConfig() *Config {
	return &c
}
